package com.gxjc.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.gxjc.model.Home;
import com.gxjc.model.Node;
import com.gxjc.service.HomeService;
import com.gxjc.service.NodeService;
import com.opensymphony.xwork2.ActionSupport;

public class HomeAction extends ActionSupport {
	@Autowired
	HomeService homeService;
	@Autowired
	NodeService nodeService;
	public String name;
	public Integer number;
	public String tojsp;
	public List<Home> list = new ArrayList<>();
	public List<Home> homes = new ArrayList<>();
	public List<Node> nodes = new ArrayList<>();
	public Integer homeId;
	public Integer nodeId;
	public String getAll(){
		list = homeService.getAll();
		return "success";
	}
	public String add(){
		Home home = new Home();
		home.setName(name);
		home.setNumber(number);
		homeService.save(home);
		tojsp = "addHome.jsp";
		return SUCCESS;
	}
	public String distribute(){
		homes = homeService.getAll();
		nodes = nodeService.getNodeBySql("from Node as n where n.home.id is null");
		tojsp = "distributeNode.jsp";
		return SUCCESS;
	}
	public String distributeNode(){		
		Node node = nodeService.getNodeById(nodeId);		
		if(node.getHome()==null){
			Home home = homeService.getHomeById(homeId);
			node.setHome(home);
			nodeService.update(node);
		}
		tojsp = "home_distribute";
		return "distribute";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getNumber() {
		return number;
	}
	public void setNumber(Integer number) {
		this.number = number;
	}
	public String getTojsp() {
		return tojsp;
	}
	public void setTojsp(String tojsp) {
		this.tojsp = tojsp;
	}
	public List<Home> getList() {
		return list;
	}
	public void setList(List<Home> list) {
		this.list = list;
	}
	public List<Home> getHomes() {
		return homes;
	}
	public void setHomes(List<Home> homes) {
		this.homes = homes;
	}
	public List<Node> getNodes() {
		return nodes;
	}
	public void setNodes(List<Node> nodes) {
		this.nodes = nodes;
	}
	public Integer getHomeId() {
		return homeId;
	}
	public void setHomeId(Integer homeId) {
		this.homeId = homeId;
	}
	public Integer getNodeId() {
		return nodeId;
	}
	public void setNodeId(Integer nodeId) {
		this.nodeId = nodeId;
	}
	
	
}
